package ch.electronet.wjw.web;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class PictureTestUtils {

	private static String[] fileStructure = { "%s/dir1/toto/dir1/1.jpg", "%s/dir1/toto/dir1/2.JpG", "%s/dir1/toto/dir1/3.JPG", "%s/dir1/toto/dir2/1.jpg", "%s/dir1/toto/dir2/10.jpg", "%s/dir1/toto/dir3/1.jpg", "%s/dir2/toto/dir3/2.jpg", "%s/dir2/toto/dir3/3.jpg", "%s/dir3/titi/wings.jpg", "%s/dir4/tata/house.jpg", "%s/root.jpg", };

	private static String[] fileStructureFail = { "%s/dir1/toto/dir2/2.png", "%s/dir1/toto/dir2/3.gif", };

	public static File prepareTemporaryFiles(String tempDirname) throws Exception {
		File tmpDir = buildTemporaryDir(tempDirname);
		tmpDir.mkdir();
		List<String> files = new LinkedList<String>();
		files.addAll(Arrays.asList(fileStructure));
		files.addAll(Arrays.asList(fileStructureFail));
		for (String path : files) {
			File file = new File(String.format(path, tmpDir.getAbsolutePath()));
			File dirname = file.getParentFile();
			if (dirname == null)
				;
			dirname.mkdirs();
			file.createNewFile();
		}
		return tmpDir;
	}

	public static void cleanTemporaryFiles(String tempDirname) throws Exception {
		FileUtils.deleteDirectory(buildTemporaryDir(tempDirname));
	}

	public static List<String> fileStructure(String tempDirname) {
		return fileStructure(fileStructure, tempDirname);
	}

	public static List<String> fileStructureFail(String tempDirname) {
		return fileStructure(fileStructureFail, tempDirname);
	}

	private static List<String> fileStructure(String[] fileStructure, String tempDirname) {
		List<String> files = new LinkedList<String>();
		for (String f : fileStructure) {
			files.add(String.format(f, tempDirname));
		}
		return files;
	}

	private static File buildTemporaryDir(String tempDirname) {
		return new File(System.getProperty("java.io.tmpdir") + File.separator + tempDirname);
	}
}
